#include "mainwindow.h"

#include <QApplication>
#include <QStyleFactory>
#include <QDebug>
#include <QLoggingCategory>
#include <iostream>
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#include <windows.h>
#endif

int main(int argc, char *argv[])
{
    // 立即输出到文件以确保能看到启动信息
    FILE* logFile = fopen("startup_log.txt", "w");
    if (logFile) {
        fprintf(logFile, "=== ALARM SYSTEM STARTUP ===\n");
        fprintf(logFile, "Program starting with %d arguments\n", argc);
        fflush(logFile);
    }
    
    std::cout << "=== ALARM SYSTEM STARTUP ===" << std::endl;
    std::cout << "Program starting with " << argc << " arguments" << std::endl;
    
    try {
        if (logFile) {
            fprintf(logFile, "Creating QApplication...\n");
            fflush(logFile);
        }
        std::cout << "Creating QApplication..." << std::endl;
        QApplication a(argc, argv);
        if (logFile) {
            fprintf(logFile, "QApplication created successfully\n");
            fflush(logFile);
        }
        std::cout << "QApplication created successfully" << std::endl;
        
#ifdef _WIN32
        std::cout << "Allocating console for Windows..." << std::endl;
        // 在Windows下分配控制台并重定向输出
        if (AllocConsole()) {
            std::cout << "Console allocated successfully" << std::endl;
            freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
            freopen_s((FILE**)stderr, "CONOUT$", "w", stderr);
            freopen_s((FILE**)stdin, "CONIN$", "r", stdin);
            
            // 设置控制台标题
            SetConsoleTitle(L"Alarm System Debug Console");
            
            if (logFile) {
                fprintf(logFile, "Console allocated and configured\n");
                fflush(logFile);
            }
            std::cout << "Console configured successfully" << std::endl;
        } else {
            if (logFile) {
                fprintf(logFile, "Failed to allocate console\n");
                fflush(logFile);
            }
            std::cout << "Failed to allocate console" << std::endl;
        }
#endif
        
        if (logFile) {
            fprintf(logFile, "Creating MainWindow...\n");
            fflush(logFile);
        }
        std::cout << "Creating MainWindow..." << std::endl;
        
        MainWindow w;
        
        if (logFile) {
            fprintf(logFile, "MainWindow created successfully\n");
            fprintf(logFile, "Showing MainWindow...\n");
            fflush(logFile);
        }
        std::cout << "MainWindow created successfully" << std::endl;
        std::cout << "Showing MainWindow..." << std::endl;
        
        w.show();
        
        if (logFile) {
            fprintf(logFile, "MainWindow shown successfully\n");
            fprintf(logFile, "Entering event loop...\n");
            fflush(logFile);
        }
        std::cout << "MainWindow shown successfully" << std::endl;
        std::cout << "Entering event loop..." << std::endl;
        
        int result = a.exec();
        
        if (logFile) {
            fprintf(logFile, "Event loop finished with result: %d\n", result);
            fflush(logFile);
        }
        std::cout << "Event loop finished with result: " << result << std::endl;
        
        return result;
        
    } catch (const std::exception& e) {
        if (logFile) {
            fprintf(logFile, "EXCEPTION: %s\n", e.what());
            fflush(logFile);
        }
        std::cout << "EXCEPTION: " << e.what() << std::endl;
        return 1;
    } catch (...) {
        if (logFile) {
            fprintf(logFile, "UNKNOWN EXCEPTION\n");
            fflush(logFile);
        }
        std::cout << "UNKNOWN EXCEPTION" << std::endl;
        return 1;
    }
    
    if (logFile) {
        fclose(logFile);
    }
}
